class Dao {
	// 打开数据库
	open() {
		return new Promise((resolve) => {
			var isOpen = plus.sqlite.isOpenDatabase({
				name: 'first',
				path: '_doc/first.db'
			})
			if (isOpen) {
				resolve(true)
			} else {
				plus.sqlite.openDatabase({
					name: 'first',
					path: '_doc/first.db',
					success: function(e) {
						console.log('openDatabase success!');
						resolve(true)

					},
					fail: function(e) {
						console.log('openDatabase failed: ' + JSON.stringify(e));
						resolve(false)
					}
				});
			}
		})
	}
	// 关闭数据库
	close() {
		return new Promise((resolve) => {
			plus.sqlite.closeDatabase({
				name: 'first',
				success: function(e) {
					console.log('closeDatabase success!');
					resolve(true)
				},
				fail: function(e) {
					console.log('closeDatabase failed: ' + JSON.stringify(e));
					resolve(false)
				}
			});
		})

	}

	// 执行SQL语句
	execute(sql) {
		return new Promise((resolve) => {
			plus.sqlite.executeSql({
				name: 'first',
				sql,
				success: function(e) {
					console.log('executeSql success!' + sql);
					resolve(true);
				},
				fail: function(e) {
					console.log('executeSql failed: ' + JSON.stringify(e));
					resolve(false)
				}
			});
		})
	}
	// 查询SQL语句
	query(sql) {
		return new Promise((resolve) => {
			plus.sqlite.selectSql({
				name: 'first',
				sql,
				success: function(data) {
					console.log(sql)
					console.log('selectSql success: ');
					console.log(data)
					resolve(data)
				},
				fail: function(e) {
					console.log('selectSql failed: ' + JSON.stringify(e));
					resolve(null)
				}
			});
		})

	}
	constructor() {

	}

}

class ResourceService {
	add({
		title,
		id,
		img,
		images
	}) {
		return this.db.execute(`insert into resource values(${id},'${title}','${img}','${images}','${new Date().valueOf()}',null)`)
	}
	delete(id) {
		return this.db.execute(`delete from resource where id = ${id}`);
	}

	get(id) {
		return new Promise(async (resolve) => {
			const data = await this.db.query(`select * from resource where id = ${id}`);
			if (data.length > 0) {
				resolve(data[0])
			} else {
				resolve(null);
			}
		})
	}
	clear() {
		return this.db.execute('delete from resource where 1=1')
	}
	drop() {
		return this.db.execute('drop table resource')
	}

	page(keyword,pageIndex, pageSize) {
		return new Promise(async (resolve) => {
			//ORDER BY `article_time` DESC ,`article_level`
			var sql =
				`select id,title,img,sort,createtime from resource where title like '%${keyword}%' order by sort asc , createtime desc limit ${pageSize} offset ${(pageIndex-1)*pageSize}`
			const data = await this.db.query(sql);
			var sql2 = `select count(id) as total from resource`;
			let total = await this.db.query(sql2);
			if (total && total.length > 0) {
				total = total[0].total;
			}
			resolve({
				msg: '',
				code: 200,
				data: {
					data,
					total,
					pageIndex,
					pageSize,
					next: total - ((pageIndex - 1) * pageSize + pageSize) > 0
				}
			})
		})


	}
	init() {
		var sql = `create table if not exists resource(
						"id"        CHAR(200)    PRIMARY    KEY    NOT    NULL,
						"title"     CHAR(500),
						"img"       CHAR(500),
						"images"    TEXT,
						"createtime" INTEGER,
						"sort" 	     INTEGER
					)`
		return this.db.execute(sql)
	}
	open() {
		return new Promise(async (resolve) => {
			console.log('开启数据库')
			await this.db.open();
			console.log('开启数据库完成')
			resolve(true)
		});

	}

	constructor() {
		this.db = new Dao();
	}
}
export default new ResourceService();
